import java.io.*;
import java.util.*;

public class Searcher {
    private File f;
    private String query;

    public void setFile (String file_path, String query) {
	this.f = new File(file_path);
	this.query = query;
    }

    public void setFile (File f, String query) {
	this.f = f;
	this.query = query;
    }

    public StringBuffer getResult() {
	StringBuffer sbf = new StringBuffer();
	if(!f.isFile()) {
	    sbf.append("File:"+ f.getPath()+" not found.");
	    return sbf;
	}

	String in_line = null;
	BufferedReader bf_reader = null;
	int query_size = query.length();
	int line = 1;
	boolean head = true;
	try {
	    bf_reader = new BufferedReader(new FileReader(f));
	    in_line = bf_reader.readLine();
	    line = 1;
	    head = true;
	    while(null != in_line) {
		if(in_line != null && (in_line.trim()).length()> query_size) {
		    if(in_line.indexOf(query) != -1) {
			if(head) {
			    sbf.append("################################\n# File:"+f.getPath()+"\n################################\n");
			    head = false;
			}
			sbf.append(line+"\t"+in_line+"\n");
		    }
		}
		in_line = in_line=bf_reader.readLine();
		line++;
	    }
	    bf_reader.close();
	}
	catch (IOException e) {
	    sbf.append("Error:search:search:401");
	}
	catch (Exception e) {
	    sbf.append("Error:search:search:402");
	}
	if(sbf.length()<1) {
	    return null;
	}
	return sbf;
    }
}



